<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\AwardModel;
use app\admin\model\MemberModel;

class Award extends Base
{
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where[] = ['awar_code', 'like', '%' . $param['searchText'] . '%'];
            }
            if (!empty($param['searchPhone'])) {
                $uid = MemberModel::where('memb_phone', 'like', '%' . $param['searchPhone'] . '%')->column('memb_id');
                $where[] = ['awar_uid', 'in', $uid];
            }

            $article = new AwardModel();
            $selectResult = $article->getUsersByWhere($where, $offset, $limit);

            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['account'] = MemberModel::where('memb_id', $vo['awar_uid'])->value('memb_phone');
                $selectResult[$key]['status'] = ['未兑奖', '已兑奖'][$vo['awar_status']];
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['awar_id'], $vo['awar_status']));
            }

            $return['total'] = $article->getAllUsers($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }

        return $this->fetch();
    }


    public function check()
    {
        $id = input('param.id');

        $article = new AwardModel();
        $flag = $article->updateStatus(['awar_status' => 1],$id);
        return json(msg($flag['code'], $flag['data'], $flag['msg']));
    }


    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id, $status)
    {
        if(!$status){
            return [
                '兑奖' => [
                    'auth' => 'award/check',
                    'href' => "javascript:updateStatus(" . $id . ")",
                    'btnStyle' => 'danger',
                    'icon' => 'fa fa-trash-o'
                ]
            ];
        }else{
            return [];
        }
    }
}
